package com.bw.module_mine.ui.login

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.get
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_common.Router
import com.bw.lib_common.base.BaseToolBarActivity
import com.bw.lib_common.base.BaseViewModel
import com.bw.lib_common.livedata_eventbus.Events
import com.bw.lib_common.livedata_eventbuss.LiveEventBus
import com.bw.module_mine.R
import com.bw.module_mine.databinding.ActivityLoginBinding
import com.bw.module_mine.ui.login.viewmodel.LoginViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

/**
 * 登录页面：
 * 1.使用navigation管理所有fragment
 * (1)主动跳转页面：
 *  activity:mBinding.navHostContainer.getFragment<NavHostFragment>().navController
 *  fragment:findNavController().navigate(R.id.action_phoneLoginFragment_to_accountLoginFragment)
 * (2)fragment和activity公用同一个viewModel
 * (3)显示不同的fragment，toolbar内容如何更新
 *
 */
@Route(path = Router.Ui.LoginActivity)
@AndroidEntryPoint
class LoginActivity : BaseToolBarActivity<LoginViewModel,ActivityLoginBinding>() {
    private val mViewModel: LoginViewModel by viewModels()
    override val mLayoutResId: Int
        get() = R.layout.activity_login
    private lateinit var mNavController: NavController


    override fun initView() {
        super.initView()
        mNavController = mBinding.navHostContainer.getFragment<NavHostFragment>().navController
    }

    override fun initListener() {
        super.initListener()
        //订阅正在显示的fragment，根据不同fragment更新toolbar的内容
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.RESUMED) {
                mViewModel.currentFragment.collect {
                    manageFragment(it)
                }
            }
        }
        //点击密码登录跳转到账号密码登录页面
        mToolbar.setOnMenuItemClickListener {
            if (it.itemId == R.id.action){
                mNavController.navigate(R.id.action_phoneLoginFragment_to_accountLoginFragment)
            }
            true
        }
    }

    private fun manageFragment(it: String) {
        title = ""
        mToolbar.menu.clear()
        when (it) {
            PhoneLoginFragment::class.simpleName -> {
                mToolbar.setNavigationIcon(R.mipmap.icon_exit)
                mToolbar.inflateMenu(R.menu.single_menu)
                mToolbar.menu.get(0).title = "密码登录"
                mToolbar.menu.get(0).icon = null
            }

            else -> {
                mToolbar.setNavigationIcon(androidx.appcompat.R.drawable.abc_ic_ab_back_material)
            }

        }
    }

    /**
     * 处理登录成功逻辑
     */
    fun handleLoginSuccess(){
        //发送全局事件
        LiveEventBus.with<Boolean>(Events.EVENT_LOGIN_SUCCESS).post(true)
        finish()

    }

}